var host = window.location.host;
var pathname = window.location.pathname;

// 
function jump2good(id, node) {
  var url = window.location.href;
  if (url.indexOf("uuid=wechat") > -1) {
    //根据返回的url 判断小程序
    wx.miniProgram.getEnv(function (res) {

      console.log("res.miniprogram:" + res.miniprogram);
      //小程序环境
      if (res.miniprogram) {
        wx.miniProgram.navigateTo({
          url: "/pages/home/goodsDetail/goodsDetail?linkId=" + id
        });
      }
    });
  } else {
    if (pathname.indexOf("/os/") != -1) {
      //app跳转
      //   $.app.goods({ goodsId: id, specialId: "" });
      node.innerHTML = `<iframe id='app_iframe' style='display:none' src="taojiji://goods?goods=${id}&t=${new Date().getTime()}"></iframe>`;
      return;
    } else {
      if (host == "a.shan666.com") {
        var goodsurl =
          "http://mtest.dabanyx.com/tjj_m.php/Tjj/Tjj/request/api_url/goods-info/display_url/Goods-detail/goods_id/" +
          id +
          "/is_asynchronous/1";
        window.location.href = goodsurl;
      } else {
        var goodsurl =
          "http://wap.taojiji.com/tjj_m.php/Tjj/Tjj/request/api_url/goods-info/display_url/Goods-detail/goods_id/" +
          id +
          "/is_asynchronous/1";
        window.location.href = goodsurl;
      }
    }
  }
}

window.app = {
  init: function (config) {
    console.log(config)
    this.id = config.id || 'app_iframe';
    this.app_name = config.app_name || 'taojiji';


  },
  t: function () {
    var myDate = new Date();
    return myDate.getTime();
  },
  jump: function (type, params) {
  this.app_name = 'taojiji';

    console.log(this)
    params = params || {};
    if ($("#" + this.id).length == 0) {
      $("<iframe style='display:none' id='" + this.id + "'></iframe>").prependTo('body');
    }
    //misscang
    var url = this.app_name + '://' + type;
    var i = 0;
    for (var key in params) {
      if (i == 0) {
        url += '?';
      } else {
        url += '&';
      }
      if (typeof (params[key]) == 'object') {
        url += key + '=' + JSON.stringify(params[key]);
      } else {
        url += key + '=' + encodeURIComponent(params[key]);
      }
      i++;
    }
    if (i == 0) {
      url += '?t=' + this.t();
    } else {
      url += '&t=' + this.t();
    }
    console.log(url);
    //alert(url);
    $("#" + this.id).attr("src", url);
  },
  //验证是否登录
  checkLogin: function () {
    this.jump('checkLogin');
  },
  //登入
  login: function (params) {
    this.jump('login', params);
  },
  //登出
  logout: function () {
    $.get(U('Users/appLogout'));
    this.jump('logout');
  }

};


//未登录
function nologin(params, node) {
  // window.app.login({
  //   "reload": 1,
  //   'type':0
  // });
  node.innerHTML = `<iframe id='app_iframe' style='display:none' src="taojiji://goods?goods=${params}&t=${new Date().getTime()}"></iframe>`;
}

//预设地址环境
function resetAddress(){
  var hostLink='',assetsLink='',type=null;
  var env=process.env.NODE_ENV;
  //在上线到正式线上打开注释
  if(env=='production'){
    hostLink="https://activity.shandjj.com/";
    assetsLink='https://mstatic.shandjj.com';
    type='onLine'
  }else{
    hostLink="http://activity.sdjj.dev/";
    assetsLink='http://mstatic.sdjj.dev';
    type='devLine'
  };
  //在上线到测试线上打开注释
  // if(env=='production'){
  //   hostLink="http://activity.sdjjia.com/";
  //   assetsLink='http://mstatic.sdjjia.com';
  //   type='testLine'
  // }else{
  //   hostLink="http://activity.sdjj.dev/";
  //   assetsLink='http://mstatic.sdjj.dev';
  //   type='devLine'
  // };
  return {hostLink,assetsLink,type};
}

export {
  jump2good,
  nologin,
  resetAddress
};

export function filterPic(e) {
  let PicList = {}
  // let baseUrl = window.location.hostname == "activity.shandjj.com" ? "http://mstatic.shandjj.com/default/ActiveImg/celebration/img/" : "http://mstatic.sdjjia.com/default/ActiveImg/celebration/img/";
  let baseUrl = window.location.hostname == "activity.shandjj.com" ? "https://mstatic.shandjj.com/default/ActiveImg/celebration/img/" : "http://mstatic.sdjjia.com/default/ActiveImg/celebration/img/";
  for (let index = 0; index < Object.keys(e).length; index++) {
    var ObjIndex =  Object.keys(e)[index];
    PicList[ObjIndex] = baseUrl + e[ObjIndex]
  }
  return PicList;
}